package com.ruoyi.quartz.task;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.CzEquipment;
import com.ruoyi.business.domain.CzEquipmentChannel;
import com.ruoyi.business.domain.CzEquipmentTrajectory;
import com.ruoyi.business.mapper.CzEquipmentChannelMapper;
import com.ruoyi.business.mapper.CzEquipmentMapper;
import com.ruoyi.business.mapper.CzEquipmentTrajectoryMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Md5Utils;

/**
 * 定位设备定时获取获取设备数据
 * 
 * @author ruoyi
 */
@Component("EquipementChannelTask")
public class EquipementChannelTask
{
	
	@Autowired
	private CzEquipmentMapper czEquipmentMapper;
	@Autowired
	private CzEquipmentChannelMapper czEquipmentChannelMapper;
	
	@Autowired
	private CzEquipmentTrajectoryMapper czEquipmentTrajectoryMapper;
	
    public void getEquChannel()
    {
    	//获取行车在线设备的定位数据
    	CzEquipment param=new CzEquipment();
    	param.setEquipmentTypeCode("VDR");
    	param.setIsOnline("online");
		List<CzEquipment> equipments=czEquipmentMapper.selectCzEquipmentList(param);
		
		//循环获取设备的通道并更新
		for(CzEquipment czEquipment : equipments) {
			handEquipent(czEquipment);
		}
    	
    }
    
    //更新数据
    public void handEquipent(CzEquipment czEquipment) {
    	List<CzEquipmentChannel> czEquipmentChannelList = new ArrayList<CzEquipmentChannel>();
//   http://8.141.173.79:18080/api/device/query/devices/czbf001/channels?count=10&page=1
		String url ="https://zlm.v0357.com:18080/api/device/query/devices/"+czEquipment.getEquipmentSerialNo()+"/channels?count=100&page=1";
		String data=HttpUtils.sendGet(url);
		Map<String, Object> dataMap=JSON.parseObject(data);
		List<Object> list=JSONArray.parseArray(dataMap.get("list").toString()); 
		for(Object object : list) {
			Map<String, Object> map =JSON.parseObject(object.toString());
			String channelPath=channelPath(map.get("deviceId").toString(),map.get("channelId").toString());
			
			CzEquipmentChannel channel = new CzEquipmentChannel();
			channel.setPath(channelPath);
			channel.setName(map.get("name").toString());
			channel.setCode(map.get("channelId").toString());
			channel.setEquipmentSerialNo(map.get("deviceId").toString());
			czEquipmentChannelList.add(channel);
		}
		czEquipmentChannelMapper.deleteCzEquipmentChannelByEquipmentSerialNo(czEquipment.getEquipmentSerialNo());
		if(czEquipmentChannelList.size()>0)
		czEquipmentChannelMapper.batchInsertCzEquipmentChannel(czEquipmentChannelList);
    }
    
    
    //获取通道地址
    public String channelPath(String equId,String channelId) {
    	String url ="https://zlm.v0357.com:18080/api/play/start/"+equId+"/"+channelId;
    	String data=HttpUtils.sendGet(url);
    	Map<String, Object> map=JSON.parseObject(data);
    	Map<String, Object> map_data=JSON.parseObject(map.get("data").toString());
    	return map_data.get("fmp4").toString();
    } 
    
    
	
    
}
